'use strict';
app.controller('programGoodsManageCtrl',['$scope','$cookies','utilService','$modal','$stateParams','toaster','programGoodsManageService',
    function($scope,$cookies,utilService,$modal,$stateParams,toaster,programGoodsManageService) {

    $scope.goods={status:1};

    //查询商品
    $scope.queryShopGoods = function () {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'goods/program',
            params:$scope.goods
        }).then(function (result) {
        });
    }

    $scope.queryShopGoods();

    //查询模块信息
    $scope.getShopList=function(){
        programGoodsManageService.getShopList($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.shops=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.getShopList();


    //打开窗口
    $scope.openPoups = function (item) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/goods/program_goods_manage_popup.html',
            controller: 'programGoodsManagePopupCtrl',
            size: 'sm',
            scope:$scope,
            resolve: {
                data: function () {
                    return  item;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    };

    //打开查看服务框
    $scope.checkReserve = function (id) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/goods/reserve_info.html',
            controller: 'reserveInfoCtrl',
            size: 'md',
            scope:$scope,
            resolve: {
                data: function () {
                    return id;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    };
}]);

app.controller('reserveInfoCtrl',['$scope','toaster','data','$modalInstance','programGoodsManageService',
    function($scope,toaster,data,$modalInstance,programGoodsManageService){
    $scope.id=data;
    $scope.goodsInfo={};

    $scope.checkReserve=function(){
        programGoodsManageService.checkReserve($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.goodsInfo=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };

    $scope.checkReserve();

    $scope.ok = function(){
        $modalInstance.dismiss('cancel');
    };

}]);


app.controller('programGoodsManagePopupCtrl', ['$scope', '$modalInstance','programGoodsManageService','toaster','data',function($scope, $modalInstance,programGoodsManageService,toaster,data) {
    //取消
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };

    $scope.ok = function () {
        $scope.addModulesGoods();
        $modalInstance.dismiss('success');
    };

    $scope.modulesAddVO={
        goodsId:data.id,
        status:1
    };
    $scope.modules=[];
    //查询模块信息
    $scope.findModules=function(){
        programGoodsManageService.findModules($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.modules=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.findModules();

    //同步至商品
    $scope.addModulesGoods=function(){
        programGoodsManageService.addModulesGoods($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    break;
                case 658:
                    toaster.pop('wranning', '提示', result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }


}]);
